package com.fenyin.samples.study.parttern.struct.compent.example1;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
  
/**
 *@Title:  安全模式的合成模式
 *<p>合成模式：具体树枝构件接口</p>
 *@Author:fengzhichao  
 *@Since:2012-9-29  
 *@Version:1.1.0
 */  
public class Window implements Container{
	
	private static final Log log = LogFactory.getLog(Window.class);
	
	private Map<String,Component> componentMap = new HashMap<String,Component>();
	
	public Window(){
		
	}

	public void draw() {
		log.info("this is window");
		for(Entry<String, Component> entry : componentMap.entrySet()){
			entry.getValue().draw();
		}
	}

	public void addComponent(String name,Component component) {
		componentMap.put(name,component);
	}

	public void removeComponent(String name) {
		componentMap.remove(name);
	}

	public Component getComponent(String name) {
		return componentMap.get(name);
	}

	public List<Component> getAllComponent() {
		List<Component> components = new ArrayList<Component>();
		for(Entry<String, Component> entry : componentMap.entrySet()){
			components.add(entry.getValue());
		}
		return components;
	}
	
	public static void main(String[] args){
		Component textComponent = new TextComponent("商品名称");
		Component textAreaComponent = new TextAreaComponent("商品详情");
		Container window = new Window();
		window.addComponent("productName", textComponent);
		window.addComponent("productDesc", textAreaComponent);
		window.draw();
	}
}
